const UserModel = require('../model/User')
// https://sequelize.org/v5/manual/querying.html
let { query } = require('../db/mysql')

class User {
  async list(ctx) {
    // todo: 应该分离sevice层
    let data = await UserModel.findAll({
      attributes: ['id', 'username'],
    })
    // let data = await query('SELECT * FROM users')
    ctx.body = {
      code: 0,
      data,
    }
  }

  async one(ctx) {
    let data = await UserModel.findAll({
      where: {
        id: ctx.params.id,
      },
    })
    // await data[0].destroy() // 删除row
    ctx.body = {
      code: 0,
      data,
    }
  }

  async create(ctx) {
    console.log('ctx: ', ctx.request.body)
    let data = await UserModel.create(ctx.request.body)
    ctx.body = {
      code: 0,
      data,
    }
  }
}

module.exports = new User()
